package at.codebase.db.entities;

import com.google.appengine.api.users.User;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Embed;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.condition.IfTrue;

/**
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 */
@Entity
public class EUser {

	@Id
	private Long userId;
	@Index
	private String username;
	private Key<EImage> imageId;
	
	@Index(IfTrue.class)
	private boolean isAdmin;				//Global admin flag
	private String adminKey;
	
	@Index
	private String googleUserId;			//Reference to the connected Google Account and Key
	private EGoogleUserData googleUserData;	//Data provided by the Google User service
	@Index
	private String userMail;
	private long timeCreate;				//The time of the create datetime
	private boolean isActive;				//True if the account is active
	private String deleteKey;
	private String locale;
	
	private String description;
	
	/*
	 * Embed classes
	 */
	@Embed
	public static class EGoogleUserData {
		String authDomain;
		String email;
		String federatedIdentity;
		String nickname;
		public String getAuthDomain() {
			return authDomain;
		}
		public String getEmail() {
			return email;
		}
		public String getFederatedIdentity() {
			return federatedIdentity;
		}
		public String getNickname() {
			return nickname;
		}
		public void setNickname(String nickname) {
			this.nickname = nickname;
		}
	}
	
	/*
	 * Objectify
	 */
	@SuppressWarnings("boxing")
	public static Key<EUser> key(Long userId) {
		return Key.create(EUser.class, userId);
	}
	
	public static Ref<EUser> ref(Long userId) {
		Key<EUser> key = key(userId);
		return Ref.create(key);
	}
	
	public static Ref<EUser> ref(EUser user) {
		return Ref.create(user);
	}
	
	public static Ref<EUser> ref(Long userId, EUser user) {
		Key<EUser> key = key(userId);
		return Ref.create(key, user);
	}
	
	/*
	 * Constructors
	 */
	public EUser() {	}
	
	private EUser(User googleUser) {	
		EGoogleUserData guserData = new EGoogleUserData();
		guserData.authDomain = googleUser.getAuthDomain();
		guserData.email = googleUser.getEmail();
		guserData.federatedIdentity = googleUser.getFederatedIdentity();
		guserData.nickname = googleUser.getNickname();
		
		this.googleUserData = guserData;
		this.googleUserId = googleUser.getUserId();
		this.userMail = googleUser.getEmail();
		this.timeCreate = System.currentTimeMillis();
		this.isActive = true;
		this.isAdmin = false;
		if(guserData.nickname.contains("@")){
			this.username = guserData.nickname.substring(0, guserData.nickname.indexOf("@"));	
		}else{
			this.username = guserData.nickname;
		}
		this.description = "";
	}
	
	public static EUser createUser(User googleUser){
		return new EUser(googleUser);
	}
	
	public static EUser createImportUser(String googleUserId, String username, String email, String description){
		EUser user = new EUser();

		EGoogleUserData guserData = new EGoogleUserData();
		guserData.authDomain = "";
		guserData.email = email;
		guserData.federatedIdentity = null;
		guserData.nickname = username;

		user.googleUserData = guserData;
		user.googleUserId = googleUserId;
		user.userMail = email;
		user.timeCreate = System.currentTimeMillis();
		user.isActive = true;
		user.isAdmin = false;
		user.username = username;
		user.description = description;
		
		return user;
	}
	
	public static EUser createExampleUser(String username, String useremail,
			String domain, boolean admin) {
		EUser user = new EUser();

		EGoogleUserData guserData = new EGoogleUserData();
		guserData.authDomain = domain;
		guserData.email = useremail;
		guserData.federatedIdentity = null;
		guserData.nickname = username;

		user.googleUserData = guserData;
		user.googleUserId = useremail;
		user.userMail = useremail;
		user.timeCreate = System.currentTimeMillis();
		user.isActive = true;
		user.isAdmin = admin;
		user.username = username;
		user.description = "User mockup...";
		return user;
	}
	
	public Long getId() {
		return userId;
	}
	
	public void setId(Long userId) {
		this.userId = userId;
	}

	public String getGoogleUserId() {
		return googleUserId;
	}

	public void setGoogleUserId(String googleUserId) {
		this.googleUserId = googleUserId;
	}

	public String getMail() {
		return userMail;
	}

	public void setMail(String userMail) {
		this.userMail = userMail;
	}

	public EGoogleUserData getGoogleUserData() {
		return googleUserData;
	}

	public void setGoogleUserData(EGoogleUserData googleUserData) {
		this.googleUserData = googleUserData;
	}

	public long getTimeCreate() {
		return timeCreate;
	}

	public void setTimeCreate(long timeCreate) {
		this.timeCreate = timeCreate;
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public void setAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}
	
	public boolean isAdmin() {
		return isAdmin;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getLocale() {
		return locale;
	}

	public void setLocale(String userLocale) {
		this.locale = userLocale;
	}

	public String getDescription() {
		if(description==null)
			return "";
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setImage(Key<EImage> imageId) {
		this.imageId = imageId;
	}
	
	public Key<EImage> getImage(){
		return imageId;
	}

	public void setDeleteKey(String key) {
		this.deleteKey = key;
	}

	public String getDeleteKey() {
		return deleteKey;
	}

	public String getAdminKey() {
		return adminKey;
	}

	public void setAdminKey(String adminKey) {
		this.adminKey = adminKey;
	}
	
}
